﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AudioCore.Entity;
using AudioCore.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

namespace AudioCore.DAO
{
    internal class ArticleDao : BaseDao
    {
        internal ArticleDao()
            : base("Article", "ArticleID")
        {
            db = DatabaseFactory.CreateDatabase(Constant.DATABASE);
        }

        internal ArticleDao(string viewName)
            : base(viewName, "ArticleID")
        {
            db = DatabaseFactory.CreateDatabase(Constant.DATABASE);
        }

        internal DataTable GetArticle(bool deleteStatus)
        {
            int status = deleteStatus ? 1 : 0;
            string query = "select * from Article where Deleted = {0}";
            query = string.Format(query, status);
            return db.ExecuteDataSet(CommandType.Text, query).Tables[0];
        }

        internal int UpdateDeleteStatus(int articleID, bool deleteStatus)
        {
            int status = deleteStatus ? 1 : 0;
            string query = "update article set Deleted = {0} where ArticleID = {1}";
            query = string.Format(query, deleteStatus, articleID);
            return int.Parse(db.ExecuteScalar(CommandType.Text, query).ToString());
        }

        internal DataTable GetDeletedArticle(int pageIndex, int pageSize, out int recordCount)
        {
            string sql = "select * from Article where ArticleID not in (select CategoryArticle.ArticleID FROM CategoryArticle INNER JOIN Category ON CategoryArticle.CategoryID = Category.CategoryID)";

            string[] fieldsName = new string[] { "*" };
            string[] equalColumn = new string[] { };
            object[] equalValue = new object[] { };
            string[] searchColumn = null;

            return SearchItemByPaging(sql, fieldsName, equalColumn, equalValue, searchColumn, string.Empty, pageIndex, pageSize, "ArticleID", out recordCount);
        }

        #region Service's method

        internal DataTable GetArticleByCategoryID(int categoryID)
        {
            object[] param = new object[] { categoryID };
            return db.ExecuteDataSet("sp_AUDIO_GetArticleByCategoryID", param).Tables[0];
        }

        /// <summary>
        /// Get list article of a category
        /// </summary>
        /// <param name="category">CategoryID</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <returns></returns>
        internal DataTable GetArticleByPage(int accountID, int categoryID, int pageIndex, int pageSize, ref int counter)
        {
            object[] param = new object[] { accountID, categoryID, (int)Constant.ItemType.Article, pageIndex, pageSize };
            DataSet dst = db.ExecuteDataSet("sp_AUDIO_GetArticleByPage", param);

            counter = int.Parse(dst.Tables[0].Rows[0][0].ToString());

            return dst.Tables[1];
        }

        internal DataTable GetTopArticle(int accountID, int numberArticle)
        {
            object[] param = new object[] { accountID, (int)Constant.ItemType.Article, numberArticle };
            DataSet dst = db.ExecuteDataSet("sp_AUDIO_GetTopArticle", param);

            return dst.Tables[0];
        }

        /// <summary>
        /// Search article by a keyword and then return item of a page which numbered by in pageIndex.
        /// </summary>
        /// <param name="keyWord">Keyword to search</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <returns></returns>
        internal DataTable SearchArticle(int accountID, string keyWord, int pageIndex, int pageSize, ref int counter)
        {
            object[] param = new object[] { accountID, (int)Constant.ItemType.Article, keyWord, pageIndex, pageSize };
            DataSet dst = db.ExecuteDataSet("sp_AUDIO_SearchArticle", param);

            counter = int.Parse(dst.Tables[0].Rows[0][0].ToString());

            return dst.Tables[1];
        }

        internal DataTable GetArticleByID(int articleID)
        {
            object[] param = new object[] { articleID };
            return db.ExecuteDataSet("sp_AUDIO_GetArticleByID", param).Tables[0];
        }


        internal DataTable GetListFavoriteArticle(int accountID, int pageIndex, int pageSize, ref int counter)
        {
            object[] param = new object[] { accountID, (int)Constant.ItemType.Article, pageIndex, pageSize };
            DataSet dst = db.ExecuteDataSet("sp_AUDIO_GetFavoriteArticle", param);

            counter = int.Parse(dst.Tables[0].Rows[0][0].ToString());

            return dst.Tables[1];
        }

        internal int UpdateRateArticle(int articleID, int ratePoint)
        {
            string sql = "Update Article SET RatePoint = {0} WHERE ArticleID = {1}";
            sql = string.Format(sql, ratePoint, articleID);
            return db.ExecuteNonQuery(CommandType.Text, sql);
        }


        internal int IncreasePlayCounter(int articleID)
        {
            string sql = "Update Article SET PlayCounter = PlayCounter + 1 WHERE ArticleID = {0}";
            sql = string.Format(sql, articleID);
            return db.ExecuteNonQuery(CommandType.Text, sql);
        }
        #endregion
    }
}
